package com.ms.education.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ms.education.course.entity.Chapter;
import com.ms.education.course.entity.Video;
import com.ms.education.course.entity.vo.ChapterSortVo;
import com.ms.education.course.mapper.ChapterMapper;
import com.ms.education.course.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.education.course.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程章节 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-01-17
 */
@Service
public class ChapterServiceImp extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    @Transactional
    @Override
    public void removeBatchByCourseIds(List<Integer> idList) {
        UpdateWrapper<Chapter> wrapper = new UpdateWrapper<>();
        wrapper.in("course_id", idList);
        this.remove(wrapper);

        //删除章节所包含的视频
        videoService.removeBatchByCourseIds(idList);
    }

    public List<Chapter> list(Integer courseId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId).orderByAsc("sort");
        List<Chapter> chapters = this.list(wrapper);
        //查询每个章节的小节
        QueryWrapper<Video> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("course_id", courseId).orderByAsc("sort");
        List<Video> videos = videoService.list(wrapper1);
        //封装每个小节到各自的章节
        for (Chapter chapter : chapters) {
            Integer chapterId = chapter.getId();
            LinkedList<Video> vs = new LinkedList<>();
            for (Video video : videos) {
                if (video.getChapterId() == chapterId) {
                    vs.add(video);
                }
            }
            chapter.setChildren(vs);
        }
        return chapters;
    }

    @Override
    @Transactional
    public void removeById(Integer id) {
        //删除章节的小节
        videoService.removeVideoByChapterId(id);
        //删除章节
        baseMapper.deleteById(id);

    }

    @Transactional
    @Override
    public void updateChapterAndVideoSort(List<ChapterSortVo> chapterSortVos) {
        //收集数据
        List<Chapter> chapters=new LinkedList<>();
        List<Video> videos=new LinkedList<>();
        chapterSortVos.forEach(ch->{
            Chapter chapter = new Chapter();
            chapter.setId(ch.getChapterId());
            chapter.setSort(ch.getSort());
            chapters.add(chapter);
            ch.getChildren().forEach(vi->{
                Video video = new Video();
                video.setId(vi.getVideoId());
                video.setSort(vi.getSort());
                video.setChapterId(ch.getChapterId());
                videos.add(video);
            });
        });
        //更新章节
        this.updateBatchById(chapters);
        //更新小节
        videoService.updateBatchById(videos);

    }
}
